﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;

namespace GtsServer.Manage.IISPage
{
    public class IISDalHelper
    {
        public bool SaveContent(HttpPostedFile file, out string newFileName)
        {
            bool isSuccess = false;
            newFileName = string.Empty;
            if (file != null)
            {
                newFileName = GetNewFileName(System.IO.Path.GetFileName(file.FileName));
                string path = "~/Content/Image/";
                if (newFileName.Split('.')[1].ToLower() == "apk")//版本
                    path = "~/Content/Version/";

                //指定上传文件在服务器上的保存路径                
                newFileName = HttpContext.Current.Server.MapPath(path + newFileName);

                //检查服务器上是否存在这个物理路径，如果不存在则创建               
                if (!System.IO.Directory.Exists(newFileName))
                {
                    file.SaveAs(newFileName);//保存文件   
                    isSuccess = true;
                }
            }
            return isSuccess;
        }

        public bool SaveContentToService(string imgPath, string oldPath = "")
        {
            byte[] contentByte = ReadFile(imgPath);
            int lastIndex = imgPath.LastIndexOf("\\") + 1;
            if (!string.IsNullOrEmpty(oldPath))
            {
                if (oldPath.IndexOf('/') > 0)
                {
                    int oldLastindex = oldPath.LastIndexOf("/") + 1;
                    oldPath = oldPath.Substring(oldLastindex);
                }
            }
            ContentService.ServiceClient client = new ContentService.ServiceClient();
            bool isSuccess = client.SaveContent(contentByte, imgPath.Substring(lastIndex), oldPath);
            //bool isSuccess = false;
            if (isSuccess)
            {
                System.IO.File.Delete(imgPath);
                return true;
            }
            return false;
        }

        public bool DeleteContent(string path)
        {
            if (!string.IsNullOrEmpty(path))
            {
                if (path.IndexOf('/') > 0)
                {
                    int oldLastindex = path.LastIndexOf("/") + 1;
                    path = path.Substring(oldLastindex);
                }
            }
            ContentService.ServiceClient client = new ContentService.ServiceClient();
            return client.DeleteByFileName(path);
        }

        public string GetNewFileName(string FileName)
        {
            Random rand = new Random();

            string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "m" +
             DateTime.Now.Day.ToString() + "d"
            + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
            + DateTime.Now.Millisecond.ToString()
                + "a" + rand.Next(1000).ToString()
            + FileName.Substring(FileName.LastIndexOf("."), FileName.Length - FileName.LastIndexOf("."));
            return newfilename;
        }

        //读取文件内容
        private byte[] ReadFile(string path)
        {
            FileStream file = File.OpenRead(path);
            byte[] content = new byte[file.Length];
            file.Read(content, 0, content.Length);
            file.Close();

            return content;
        }
    }
}